package com.zcg.blogger.webCollector.Extractor;

import java.util.regex.Pattern;

import javax.annotation.Resource;



import org.springframework.beans.factory.annotation.Autowired;

import com.zcg.blogger.dao.CSDNArticleDao;
import com.zcg.blogger.model.CSDNArticle;

import cn.edu.hfut.dmic.webcollector.extract.Extractor;
import cn.edu.hfut.dmic.webcollector.extract.ExtractorParams;
import cn.edu.hfut.dmic.webcollector.model.Page;

public class CSDNHotArticleExtractor extends Extractor {

	private CSDNArticle csdnArticle;

	private CSDNArticleDao CSDNArticleDao;
	
	public CSDNHotArticleExtractor(Page page, ExtractorParams params) {
		super(page, params);
		CSDNArticleDao = (CSDNArticleDao) params.get("CSDNArticleDao");
	}

	@Override
	public void extract() throws Exception {
		String linkView = selectText("span[class=link_view]");
		int num = Integer.parseInt(linkView.substring(0, linkView.length() - 3));
		if(num <= 30000){
			return;
		}
		csdnArticle = new CSDNArticle();
		csdnArticle.setTitle(selectText("span[class=link_title]"));
		csdnArticle.setUrl(getUrl());
		csdnArticle.setNum(num);
		csdnArticle.setContent(selectText("div[class=article_content]").substring(0,200) +"..."); 
		csdnArticle.setDate(selectText("span[class=link_postdate]"));
	}

	@Override
	public void output() throws Exception {
		if(csdnArticle == null || csdnArticle.getNum() <= 30000){
			return;
		}
		CSDNArticleDao.insert(csdnArticle);
	}

	@Override
	public boolean shouldExecute() {
		return Pattern.matches(
				"http://blog.csdn.net/[a-zA-Z0-9]+/article/details/[0-9]+", getUrl());
	}

}
